package com.gnerv.battle.boot.module.base.service.impl;

import com.gnerv.battle.boot.module.base.model.bo.AccountBO;
import com.gnerv.battle.boot.module.base.model.dto.AccountDTO;
import com.gnerv.battle.boot.module.base.model.dto.AccountDetailsDTO;
import com.gnerv.battle.boot.module.base.model.entity.Account;
import com.gnerv.battle.boot.module.base.repository.mapper.AccountMapper;
import com.gnerv.battle.boot.module.base.repository.mapper.RoleAccountMapper;
import com.gnerv.battle.boot.module.base.repository.mapper.RoleMapper;
import com.gnerv.battle.boot.module.base.service.AccountService;
import com.gnerv.battle.boot.module.base.service.CurrentAccountService;
import com.gnerv.battle.boot.module.base.service.PasswordService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

import static com.gnerv.battle.boot.common.tools.PageTools.limitSize;
import static com.gnerv.battle.boot.common.tools.PageTools.limitStart;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class AccountServiceImpl implements AccountService {

    private final AccountMapper accountMapper;
    private final RoleMapper roleMapper;
    private final RoleAccountMapper roleAccountMapper;

    private final PasswordService passwordService;
    private final CurrentAccountService currentAccountService;

    @Override
    public boolean create(AccountBO accountBO) {
        Account account = accountBO.toCreateAccount();
        account.setPassword(passwordService.encrypt(account.getPassword()));
        account.setCreator(currentAccountService.accountUkId());
        accountMapper.insert(account);
        return true;
    }

    @Override
    public boolean deleteByUkId(String ukId) {
        accountMapper.deleteByUkId(ukId);
        roleAccountMapper.deleteByAccountUkId(ukId);
        return true;
    }

    @Override
    public boolean updateByUkId(AccountBO accountBO) {
        Account account = accountBO.toUpdateAccount();
        accountMapper.updateByUkId(account);
        return true;
    }

    @Override
    public AccountDTO selectByUkId(String ukId) {
        return accountMapper.selectByUkId(ukId);
    }

    @Override
    public AccountDTO selectByUsername(String username) {
        return accountMapper.selectByUsername(username);
    }

    @Override
    public AccountDetailsDTO selectLoginByUsername(String username) {
        return accountMapper.selectLoginByUsername(username);
    }

    @Override
    public List<AccountDTO> listByUsername(String username, Integer page, Integer size) {
        return accountMapper.listByUsername(username, limitStart(page, size), limitSize(size));
    }

    @Override
    public List<AccountDTO> listByRoleUkId(String roleUkId) {
        return accountMapper.listByRoleUkId(roleUkId);
    }

    @Override
    public List<AccountDTO> listByOrgUkId(String orgUkId) {
        return accountMapper.listByOrgUkId(orgUkId);
    }

    @Override
    public int countByUsername(String username) {
        return accountMapper.countByUsername(username);
    }

    @Override
    public boolean bindRole(@NonNull String accountUkId, @NonNull Set<String> roleUkIdList) {
        roleAccountMapper.insertAccountRole(accountUkId, roleUkIdList, currentAccountService.accountUkId());
        return true;
    }

    @Override
    public boolean unbindRole(@NonNull String accountUkId) {
        roleAccountMapper.deleteByAccountUkId(accountUkId);
        return true;
    }

    @Override
    public boolean isExist(String username) {
        return accountMapper.isExist(username) == 1;
    }

    @Override
    public Set<String> listUkIdByRoleUkIdAndOrgUkId(Set<String> roleUkIdSet, Set<String> orgUkIdSet) {
        return accountMapper.listUkIdByRoleUkIdAndOrgUkId(roleUkIdSet, orgUkIdSet);
    }

}




